package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.JTextComponent;

import org.joda.time.DateTime;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.controller.UIRootController;
import vcp.client.GUI.mvc.view.custom.CustomInputVerifier;
import vcp.client.GUI.mvc.view.custom.CustomJSpinnerDate;
import vcp.client.GUI.mvc.view.effects.ValidationLayerUI;
import vcp.client.forms.SubscriptionForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common;
import vcp.client.utility.Common.RegexStrings;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.entity.Subscription;
import vcp.enums.FormType;
import vcp.enums.PopUpMessageType;
import vcp.enums.SiteEnum;
import vcp.enums.SubscriptionType;

/**
 * <p>
 * user interface subscription view class
 * </p>
 * 
 * @author VCP team
 * 
 */
public class UISubscriptionView extends UIAbstractView implements KeyListener, ItemListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4107444961361100063L;

	private static final String PANEL_ID = "pID";
	private static final String PANEL_CAR_ID = "pCarID";
	private static final String PANEL_START_DATE = "pStartDate";
	private static final String PANEL_SITE = "pSite";
	private static final String PANEL_ESTIMATED_LEAVE_TIME = "pEstimatedLeaveTime";
	private static final String PANEL_EMAIL = "pEmail";
	private static final String PANEL_ARRIVAL_DATE = "pArrivalDate";
	private static final String PANEL_SUBSCRIPTION = "pSubscription";

	private HashMap <String, JPanel> panelMap;

	/**
	 * getter
	 * 
	 * @return panel map
	 */
	private HashMap <String, JPanel> getPanelMap ()
	{
		if ( this.panelMap == null )
		{
			this.panelMap = new HashMap <>();
		}
		return this.panelMap;
	}

	private ButtonGroup radioButtonGroup;

	private JRadioButton rdbtnFullSubscription;
	private JRadioButton rdbtnCasualSubscription;
	private JRadioButton rdbtnOrderAheadSubscription;

	private JRadioButton selectedRadioButton;

	private JButton btnSubmit;
	private JTextField tfID;
	private CustomJSpinnerDate spStartDate;
	private JFormattedTextField ftfCarID;

	private CustomJSpinnerDate spEstimatedDate;
	private JFormattedTextField ftfEmail;
	private CustomJSpinnerDate spArrivalDate;

	private JComboBox <Integer> cbSubscriber;
	private JComboBox <SiteEnum> cbSite;

	private ValidationLayerUI emailValidationLayerUI;
	private ValidationLayerUI carIDValidationLayerUI;

	private IFormSubmitionListener <SubscriptionForm> formSubmitionListener;

	/**
	 * setter
	 * 
	 * @param formSubmitionListener
	 */
	public void setFormSubmitionListener ( IFormSubmitionListener <SubscriptionForm> formSubmitionListener )
	{
		this.formSubmitionListener = formSubmitionListener;
	}

	/**
	 * constructor
	 */
	public UISubscriptionView ()
	{
		super();
		this.prepareFullSubscriberView();
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * method resets to given date
	 * 
	 * @param currentDateTime
	 */
	public void resetDates ( DateTime currentDateTime )
	{
		this.spArrivalDate.resetModel(currentDateTime);
		this.spEstimatedDate.resetModel(currentDateTime);
		this.spStartDate.resetModel(currentDateTime);
	}

	/**
	 * method loads subscriptions
	 * 
	 * @param subscribtionsID
	 */
	public void loadSusbscriptions ( List <Integer> subscribtionsID )
	{
		this.cbSubscriber.setModel(new DefaultComboBoxModel <>(subscribtionsID.toArray(new Integer[subscribtionsID.size()])));
		if ( subscribtionsID.size() == 1 )
		{
			this.cbSubscriber.setEnabled(false);
		}
	}

	/**
	 * method loads customer id
	 * 
	 * @param id
	 */
	public void loadCustomerID ( int id )
	{
		this.tfID.setText(String.valueOf(id));
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * method prepares casual subscribers view elements
	 */
	private void prepareCasualSubscriberView ()
	{
		this.getPanelMap().get(UISubscriptionView.PANEL_START_DATE).setVisible(true);
		this.getPanelMap().get(UISubscriptionView.PANEL_ESTIMATED_LEAVE_TIME).setVisible(true);

		this.getPanelMap().get(UISubscriptionView.PANEL_ARRIVAL_DATE).setVisible(false);

		this.cbSite.setEnabled(true);
	}

	/**
	 * method prepares full subscribers view elements
	 */
	private void prepareFullSubscriberView ()
	{
		this.getPanelMap().get(UISubscriptionView.PANEL_START_DATE).setVisible(true);

		this.getPanelMap().get(UISubscriptionView.PANEL_ESTIMATED_LEAVE_TIME).setVisible(false);
		this.getPanelMap().get(UISubscriptionView.PANEL_ARRIVAL_DATE).setVisible(false);

		this.cbSite.setEnabled(false);
	}

	/**
	 * method prepares order ahead subscribers view elements
	 */
	private void prepareOrderAheadSubscriberView ()
	{
		this.getPanelMap().get(UISubscriptionView.PANEL_START_DATE).setVisible(false);
		this.getPanelMap().get(UISubscriptionView.PANEL_ESTIMATED_LEAVE_TIME).setVisible(true);
		this.getPanelMap().get(UISubscriptionView.PANEL_ARRIVAL_DATE).setVisible(true);

		this.cbSite.setEnabled(true);
	}

	/**
	 * getter
	 * 
	 * @return site
	 */
	private SiteEnum getSelectedSite ()
	{
		return (SiteEnum) this.cbSite.getSelectedItem();
	}

	/**
	 * getter
	 * 
	 * @return subscription
	 */
	private Integer getSelectedSubscribtion ()
	{
		return (Integer) this.cbSubscriber.getSelectedItem();
	}

	/**
	 * getter
	 * 
	 * @return subscription type
	 */
	private SubscriptionType getSelectedSubscriptionType ()
	{
		SubscriptionType type = SubscriptionType.ORDER_AHEAD;
		switch ( this.selectedRadioButton.getName() )
		{
			case Common.RBTN_FULL_SUBSCRIBER:
				type = SubscriptionType.FULL_SUBSCRIBER;
				break;
			case Common.RBTN_CASUAL_SUBSCRIBER:
				type = SubscriptionType.CASUAL_SUBSCRIBER;
				break;
		}
		return type;
	}

	/**
	 * checks if subscribers info is valid
	 * 
	 * @return true if valid
	 */
	private boolean isSubscriberInfoValid ()
	{
		return this.ftfCarID.getInputVerifier().verify(this.ftfCarID)
				&& this.ftfEmail.getInputVerifier().verify(this.ftfEmail);
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public void resetView ()
	{
		this.radioButtonGroup.setSelected(this.rdbtnOrderAheadSubscription.getModel(), true);

		this.selectedRadioButton = this.rdbtnOrderAheadSubscription;
		this.btnSubmit.setEnabled(false);

		this.ftfCarID.setText("");
		this.ftfEmail.setText("");
	}

	@Override
	public void clearView ()
	{
		this.resetView();
		this.onScreen = false;
		this.rdbtnOrderAheadSubscription.setSelected(true);
		this.prepareOrderAheadSubscriberView();
	}

	@Override
	protected void setupLayout ()
	{
		this.setPreferredSize(new Dimension(908, 704));
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel_SubscriptionType = new JPanel();
		panel_SubscriptionType.setPreferredSize(new Dimension(10, 125));
		this.add(panel_SubscriptionType, BorderLayout.NORTH);
		panel_SubscriptionType.setLayout(new BorderLayout(0, 0));

		final JPanel panel_Subscription = new JPanel();
		panel_Subscription.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Subscription Type", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_SubscriptionType.add(panel_Subscription, BorderLayout.WEST);
		this.getPanelMap().put(UISubscriptionView.PANEL_SUBSCRIPTION, panel_Subscription);
		panel_Subscription.setLayout(new BoxLayout(panel_Subscription, BoxLayout.Y_AXIS));

		final JLabel lblNewLabel = new JLabel("Choose Subscription:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_Subscription.add(lblNewLabel);

		this.radioButtonGroup = new ButtonGroup();

		this.rdbtnOrderAheadSubscription = new JRadioButton(Common.RBTN_ORDER_AHEAD_SUBSCRIBER);
		this.rdbtnOrderAheadSubscription.setName(Common.RBTN_ORDER_AHEAD_SUBSCRIBER);
		this.rdbtnOrderAheadSubscription.setFocusable(false);
		this.rdbtnOrderAheadSubscription.setSelected(true);
		this.rdbtnOrderAheadSubscription.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.radioButtonGroup.add(this.rdbtnOrderAheadSubscription);
		panel_Subscription.add(this.rdbtnOrderAheadSubscription);

		this.rdbtnCasualSubscription = new JRadioButton(Common.RBTN_CASUAL_SUBSCRIBER);
		this.rdbtnCasualSubscription.setName(Common.RBTN_CASUAL_SUBSCRIBER);
		this.rdbtnCasualSubscription.setFocusable(false);
		this.radioButtonGroup.add(this.rdbtnCasualSubscription);
		panel_Subscription.add(this.rdbtnCasualSubscription);
		this.rdbtnCasualSubscription.setFont(new Font("Tahoma", Font.PLAIN, 16));
		this.rdbtnFullSubscription = new JRadioButton(Common.RBTN_FULL_SUBSCRIBER);
		this.rdbtnFullSubscription.setName(Common.RBTN_FULL_SUBSCRIBER);
		this.rdbtnFullSubscription.setFocusable(false);
		this.radioButtonGroup.add(this.rdbtnFullSubscription);
		panel_Subscription.add(this.rdbtnFullSubscription);
		this.rdbtnFullSubscription.setFont(new Font("Tahoma", Font.PLAIN, 16));

		final JPanel panel_5 = new JPanel();
		panel_5.setPreferredSize(new Dimension(10, 60));
		this.add(panel_5, BorderLayout.SOUTH);
		panel_5.setLayout(new GridLayout(0, 5, 0, 0));

		final JPanel panel_7 = new JPanel();
		final FlowLayout fl_panel_7 = (FlowLayout) panel_7.getLayout();
		fl_panel_7.setAlignment(FlowLayout.RIGHT);
		panel_5.add(panel_7);

		final JPanel panel_6 = new JPanel();
		panel_5.add(panel_6);

		JPanel panel_2 = new JPanel();
		panel_5.add(panel_2);

		JPanel panel_8 = new JPanel();
		panel_5.add(panel_8);

		this.btnSubmit = new JButton(Common.BTN_SUBMIT);
		panel_5.add(this.btnSubmit);
		this.btnSubmit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnSubmit.setName(Common.BTN_SUBMIT);
		this.btnSubmit.setFont(new Font("Lucida Grande", Font.PLAIN, 16));

		JPanel panel_Controls = new JPanel();
		this.add(panel_Controls, BorderLayout.WEST);
		panel_Controls.setLayout(new GridLayout(11, 1, 0, 0));

		JPanel pID = new JPanel();
		pID.setPreferredSize(new Dimension(500, 35));
		panel_Controls.add(pID);
		this.getPanelMap().put(UISubscriptionView.PANEL_ID, pID);
		pID.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));

		final JPanel panel_3 = new JPanel();
		final FlowLayout flowLayout_7 = (FlowLayout) panel_3.getLayout();
		flowLayout_7.setAlignment(FlowLayout.RIGHT);
		panel_3.setPreferredSize(new Dimension(200, 35));
		pID.add(panel_3);

		final JLabel lblId = new JLabel("ID:");
		lblId.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_3.add(lblId);

		final JPanel panel_4 = new JPanel();
		pID.add(panel_4);

		this.tfID = new JTextField();
		this.tfID.setPreferredSize(new Dimension(150, 30));
		this.tfID.setFont(new Font("Tahoma", Font.PLAIN, 18));
		this.tfID.setEnabled(false);
		this.tfID.setEditable(false);
		panel_4.add(this.tfID);
		this.tfID.setColumns(10);

		JPanel pSubscription = new JPanel();
		panel_Controls.add(pSubscription);
		FlowLayout flowLayout_8 = (FlowLayout) pSubscription.getLayout();
		flowLayout_8.setVgap(1);
		flowLayout_8.setAlignment(FlowLayout.LEFT);
		pSubscription.setPreferredSize(new Dimension(500, 35));

		JPanel panel = new JPanel();
		FlowLayout flowLayout_9 = (FlowLayout) panel.getLayout();
		flowLayout_9.setAlignment(FlowLayout.RIGHT);
		panel.setPreferredSize(new Dimension(200, 35));
		pSubscription.add(panel);

		JLabel lblNewLabel_2 = new JLabel("Subscription ID:");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel.add(lblNewLabel_2);

		JPanel panel_1 = new JPanel();
		pSubscription.add(panel_1);

		this.cbSubscriber = new JComboBox <>();
		this.cbSubscriber.setPreferredSize(new Dimension(80, 35));
		this.cbSubscriber.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_1.add(this.cbSubscriber);

		JPanel pCarID = new JPanel();
		pCarID.setPreferredSize(new Dimension(500, 35));
		panel_Controls.add(pCarID);
		this.getPanelMap().put(UISubscriptionView.PANEL_CAR_ID, pCarID);
		pCarID.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));

		final JPanel panel_9 = new JPanel();
		final FlowLayout flowLayout_6 = (FlowLayout) panel_9.getLayout();
		flowLayout_6.setAlignment(FlowLayout.RIGHT);
		panel_9.setPreferredSize(new Dimension(200, 35));
		pCarID.add(panel_9);

		final JLabel lblCarId = new JLabel("Car ID:");
		lblCarId.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_9.add(lblCarId);

		final JPanel panel_10 = new JPanel();
		pCarID.add(panel_10);

		this.ftfCarID = new JFormattedTextField();
		JLayer <JTextComponent> carIDLayer = new JLayer <JTextComponent>(this.ftfCarID);
		this.carIDValidationLayerUI = new ValidationLayerUI();
		carIDLayer.setUI(this.carIDValidationLayerUI);
		this.ftfCarID.setToolTipText("Format:#######");
		this.ftfCarID.setPreferredSize(new Dimension(150, 30));
		this.ftfCarID.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_10.add(carIDLayer);

		JPanel pEmail = new JPanel();
		pEmail.setPreferredSize(new Dimension(500, 35));
		panel_Controls.add(pEmail);
		this.getPanelMap().put(UISubscriptionView.PANEL_EMAIL, pEmail);
		pEmail.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));

		final JPanel panel_17 = new JPanel();
		final FlowLayout flowLayout_2 = (FlowLayout) panel_17.getLayout();
		flowLayout_2.setAlignment(FlowLayout.RIGHT);
		panel_17.setPreferredSize(new Dimension(200, 35));
		pEmail.add(panel_17);

		final JLabel lblEmail = new JLabel("E-Mail:");
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_17.add(lblEmail);

		final JPanel panel_18 = new JPanel();
		pEmail.add(panel_18);

		this.ftfEmail = new JFormattedTextField();
		JLayer <JTextComponent> emailLayer = new JLayer <JTextComponent>(this.ftfEmail);
		this.emailValidationLayerUI = new ValidationLayerUI();
		emailLayer.setUI(this.emailValidationLayerUI);
		this.ftfEmail.setToolTipText("$$$@$$$.$$$");
		this.ftfEmail.setFont(new Font("Tahoma", Font.PLAIN, 18));
		this.ftfEmail.setPreferredSize(new Dimension(250, 30));
		panel_18.add(emailLayer);

		JPanel pSite = new JPanel();
		pSite.setPreferredSize(new Dimension(500, 35));
		panel_Controls.add(pSite);
		this.getPanelMap().put(UISubscriptionView.PANEL_SITE, pSite);
		pSite.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));

		final JPanel panel_13 = new JPanel();
		final FlowLayout flowLayout_4 = (FlowLayout) panel_13.getLayout();
		flowLayout_4.setAlignment(FlowLayout.RIGHT);
		panel_13.setPreferredSize(new Dimension(200, 35));
		pSite.add(panel_13);

		final JLabel lblSite = new JLabel("Site:");
		lblSite.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_13.add(lblSite);

		final JPanel panel_14 = new JPanel();
		pSite.add(panel_14);

		this.cbSite = new JComboBox <>();
		this.cbSite.setModel(new DefaultComboBoxModel <>(SiteEnum.values()));
		this.cbSite.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_14.add(this.cbSite);

		JPanel pStartDate = new JPanel();
		pStartDate.setPreferredSize(new Dimension(500, 35));
		this.getPanelMap().put(UISubscriptionView.PANEL_START_DATE, pStartDate);
		panel_Controls.add(pStartDate);
		pStartDate.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));

		final JPanel panel_11 = new JPanel();
		final FlowLayout flowLayout_5 = (FlowLayout) panel_11.getLayout();
		flowLayout_5.setAlignment(FlowLayout.RIGHT);
		panel_11.setPreferredSize(new Dimension(200, 35));
		pStartDate.add(panel_11);

		final JLabel lblStartDate = new JLabel("Start Date:");
		lblStartDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_11.add(lblStartDate);

		final JPanel panel_12 = new JPanel();
		pStartDate.add(panel_12);

		this.spStartDate = new CustomJSpinnerDate(new DateTime(), this);
		this.spStartDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_12.add(this.spStartDate);

		JPanel pArrivalDate = new JPanel();
		pArrivalDate.setPreferredSize(new Dimension(500, 35));
		panel_Controls.add(pArrivalDate);
		this.getPanelMap().put(UISubscriptionView.PANEL_ARRIVAL_DATE, pArrivalDate);
		pArrivalDate.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));

		final JPanel panel_19 = new JPanel();
		final FlowLayout flowLayout_1 = (FlowLayout) panel_19.getLayout();
		flowLayout_1.setAlignment(FlowLayout.RIGHT);
		panel_19.setPreferredSize(new Dimension(200, 35));
		pArrivalDate.add(panel_19);

		final JLabel lblArrivalDate = new JLabel("Arrival Date:");
		lblArrivalDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_19.add(lblArrivalDate);

		final JPanel panel_20 = new JPanel();
		pArrivalDate.add(panel_20);

		this.spArrivalDate = new CustomJSpinnerDate(new DateTime(), this);
		this.spArrivalDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_20.add(this.spArrivalDate);

		JPanel pEstimatedTime = new JPanel();
		panel_Controls.add(pEstimatedTime);
		pEstimatedTime.setPreferredSize(new Dimension(500, 35));
		this.getPanelMap().put(UISubscriptionView.PANEL_ESTIMATED_LEAVE_TIME, pEstimatedTime);
		pEstimatedTime.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));

		final JPanel panel_15 = new JPanel();
		final FlowLayout flowLayout_3 = (FlowLayout) panel_15.getLayout();
		flowLayout_3.setAlignment(FlowLayout.RIGHT);
		panel_15.setMinimumSize(new Dimension(90, 20));
		panel_15.setPreferredSize(new Dimension(200, 35));
		pEstimatedTime.add(panel_15);

		final JLabel lblEstimatedTime = new JLabel("Estimated Leave Date:");
		lblEstimatedTime.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_15.add(lblEstimatedTime);

		final JPanel panel_16 = new JPanel();
		pEstimatedTime.add(panel_16);

		this.spEstimatedDate = new CustomJSpinnerDate(new DateTime(), this);
		this.spEstimatedDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_16.add(this.spEstimatedDate);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnSubmit.addActionListener(this);

		this.ftfEmail.addKeyListener(this);
		this.ftfCarID.addKeyListener(this);

		this.rdbtnFullSubscription.addItemListener(this);
		this.rdbtnCasualSubscription.addItemListener(this);
		this.rdbtnOrderAheadSubscription.addItemListener(this);

		Pattern carIDPattern = UIRootController.DEBUG
														? Pattern.compile(RegexStrings.ACCEPT_ALL)
														: Pattern.compile(RegexStrings.CAR_ID_7_DIGITS);
		this.ftfCarID.setInputVerifier(new CustomInputVerifier(this.carIDValidationLayerUI, carIDPattern));

		Pattern emailPattern = UIRootController.DEBUG
														? Pattern.compile(RegexStrings.ACCEPT_ALL)
														: Pattern.compile(RegexStrings.EMAIL);
		this.ftfEmail.setInputVerifier(new CustomInputVerifier(this.emailValidationLayerUI, emailPattern));
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_SUBSCRIBER, 46, 46);
		if ( icon != null )
		{
			this.btnSubmit.setIcon(icon);
			this.btnSubmit.setIconTextGap(iconTextGap);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		if ( this.formSubmitionListener != null )
		{
			SubscriptionForm filledForm = null;

			if ( this.isSubscriberInfoValid() )
			{
				Integer customerID = Integer.valueOf(this.tfID.getText());
				Integer subscriptionID = this.getSelectedSubscribtion();
				Integer carID = Integer.valueOf(this.ftfCarID.getText());

				SiteEnum site = this.getSelectedSite();
				DateTime startDate = null;
				DateTime endDate = null;
				DateTime arrivalDate = null;
				DateTime estimatedLeaveDate = null;

				switch ( this.selectedRadioButton.getName() )
				{
					case Common.RBTN_FULL_SUBSCRIBER:
						site = null;
					case Common.RBTN_CASUAL_SUBSCRIBER:
						startDate = new DateTime(this.spStartDate.getModel().getValue());
						endDate = startDate.plusDays(28);

						int option = JOptionPane.showConfirmDialog(null, String.format(UserNotification.USER_ALREADY_HAS__SUBSCRIBTION__ADD_NEW__CAR__, subscriptionID, carID));
						if ( option != JOptionPane.OK_OPTION )
						{
							subscriptionID = null;
						}
						break;
					case Common.RBTN_ORDER_AHEAD_SUBSCRIBER:
						subscriptionID = null;
						arrivalDate = new DateTime(this.spArrivalDate.getModel().getValue());
						estimatedLeaveDate = new DateTime(this.spEstimatedDate.getModel().getValue());
						break;
				}

				Subscription newSubscription = new Subscription();
				newSubscription.setCustomerID(customerID);
				newSubscription.setSite(site);
				newSubscription.setSubscriptionType(this.getSelectedSubscriptionType());
				newSubscription.setStartDate(startDate);
				newSubscription.setEndDate(endDate);
				newSubscription.setArrivalDate(arrivalDate);
				newSubscription.setEstimatedLeaveDate(estimatedLeaveDate);

				filledForm = new SubscriptionForm(FormType.SUBSCRIPTION, newSubscription, subscriptionID, carID);
			}

			if ( filledForm != null )
			{
				this.formSubmitionListener.onFormSubmitted(new FormSubmitionEvent <SubscriptionForm>(this, filledForm));
			}
			else
			{
				this.controllerDelegate.showPopUpMessage(UserNotification.PROBLEM_ACCORD_WITH_FORM_SUBMIT, PopUpMessageType.ERROR);
			}
		}
	}

	/****************************************************************************************************************************
	 *** KeyListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void keyPressed ( KeyEvent event )
	{
	}

	@Override
	public void keyReleased ( KeyEvent event )
	{
		String fieldCarIDText = this.ftfCarID.getText();
		String fieldEmailText = this.ftfEmail.getText();

		boolean result = UtilityHelper.getUtilityHelperInstance().isParamsNotNull(fieldEmailText, fieldCarIDText);
		this.btnSubmit.setEnabled(result);
	}

	@Override
	public void keyTyped ( KeyEvent event )
	{
	}

	/****************************************************************************************************************************
	 *** ItemListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void itemStateChanged ( ItemEvent event )
	{
		if ( this.onScreen )
		{
			if ( ItemEvent.SELECTED == event.getStateChange() )
			{
				this.selectedRadioButton = (JRadioButton) event.getSource();
				switch ( this.selectedRadioButton.getName() )
				{
					case Common.RBTN_FULL_SUBSCRIBER:
						this.prepareFullSubscriberView();
						break;
					case Common.RBTN_CASUAL_SUBSCRIBER:
						this.prepareCasualSubscriberView();
						break;
					case Common.RBTN_ORDER_AHEAD_SUBSCRIBER:
						this.prepareOrderAheadSubscriberView();
						break;
				}
			}
		}
	}

}
